//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LatticeRsg.h"
using namespace Cephei::QL::Math::Randomnumbers;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Randomnumbers::CLatticeRsg::CLatticeRsg (UInt64 dimensionality, Cephei::IVector<Double>^ z, UInt64 N) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<Double>^ _Cz;
    try
    {
#ifdef HANDLE
        _phLatticeRsg = NULL;
#endif
        QuantLib::Size _dimensionality = (QuantLib::Size)ValueHelper::Convert (dimensionality);
        CoVector<Double>^ _Cz = safe_cast<CoVector<Double>^> (z);
        _Cz->Lock();
        INativeVector<Double>^ _NCIz = _Cz->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCz = safe_cast<CDoubleVector^>(_NCIz);
        std::vector<QuantLib::Real>& _z = static_cast<std::vector<QuantLib::Real>&> (_NCz->GetReference ());
        QuantLib::Size _N = (QuantLib::Size)ValueHelper::Convert (N);
        _ppLatticeRsg = new boost::shared_ptr<QuantLib::LatticeRsg> (new QuantLib::LatticeRsg ( _dimensionality,  _z,  _N ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cz != nullptr) _Cz->Unlock();
    }
}
Cephei::QL::Math::Randomnumbers::CLatticeRsg::CLatticeRsg (boost::shared_ptr<QuantLib::LatticeRsg>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLatticeRsg = NULL;
#endif
	_ppLatticeRsg = &childNative;
    
}
Cephei::QL::Math::Randomnumbers::CLatticeRsg::CLatticeRsg (QuantLib::LatticeRsg& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLatticeRsg = NULL;
#endif
	_ppLatticeRsg = new boost::shared_ptr<QuantLib::LatticeRsg> (&childNative);
    
    _LatticeRsgOwner = owner;
    
}

Cephei::QL::Math::Randomnumbers::CLatticeRsg::CLatticeRsg (CLatticeRsg^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLatticeRsg = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLatticeRsg = new boost::shared_ptr<QuantLib::LatticeRsg> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Randomnumbers::CLatticeRsg::CLatticeRsg (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLatticeRsg = NULL;
#endif
	if (!t->IsSubclassOf(CLatticeRsg::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Randomnumbers::CLatticeRsg::CLatticeRsg (QuantLib::Handle<QuantLib::LatticeRsg>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLatticeRsg = &childNative;
	_ppLatticeRsg = &static_cast<boost::shared_ptr<QuantLib::LatticeRsg>>(childNative.currentLink());
    
    _LatticeRsgOwner = owner;
}
Cephei::QL::Math::Randomnumbers::CLatticeRsg::CLatticeRsg (QuantLib::Handle<QuantLib::LatticeRsg> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLatticeRsg = &childNative;
	_ppLatticeRsg = &static_cast<boost::shared_ptr<QuantLib::LatticeRsg>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Randomnumbers::CLatticeRsg::CLatticeRsg (QuantLib::LatticeRsg childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLatticeRsg = NULL;
#endif
	_ppLatticeRsg = new boost::shared_ptr<QuantLib::LatticeRsg> (new QuantLib::LatticeRsg (childNative));
    
}
#endif

Cephei::QL::Math::Randomnumbers::CLatticeRsg::~CLatticeRsg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLatticeRsg != NULL)
    {
	    delete _ppLatticeRsg;
        _ppLatticeRsg = NULL;
    }
}
Cephei::QL::Math::Randomnumbers::CLatticeRsg::!CLatticeRsg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLatticeRsg != NULL)
    {
	    delete _ppLatticeRsg;
    }
}
QuantLib::LatticeRsg& Cephei::QL::Math::Randomnumbers::CLatticeRsg::GetReference ()
{
    if (_ppLatticeRsg == NULL) throw gcnew NativeNullException ();
	return **_ppLatticeRsg;
}
boost::shared_ptr<QuantLib::LatticeRsg>& Cephei::QL::Math::Randomnumbers::CLatticeRsg::GetShared ()
{
    if (_ppLatticeRsg == NULL) throw gcnew NativeNullException ();
	return *_ppLatticeRsg;
}
QuantLib::LatticeRsg* Cephei::QL::Math::Randomnumbers::CLatticeRsg::GetPointer ()
{
    if (_ppLatticeRsg == NULL) throw gcnew NativeNullException ();
	return &**_ppLatticeRsg;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LatticeRsg>& Cephei::QL::Math::Randomnumbers::CLatticeRsg::GetHandle ()
{
	if (_phLatticeRsg == NULL)
	{
		_phLatticeRsg = new Handle<QuantLib::LatticeRsg> (*_ppLatticeRsg);
	}
	return *_phLatticeRsg;
}
#endif
bool Cephei::QL::Math::Randomnumbers::CLatticeRsg::HasNative () 
{
	return (_ppLatticeRsg != NULL);
}

UInt64 Cephei::QL::Math::Randomnumbers::CLatticeRsg::Dimension::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLatticeRsg)->dimension ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Randomnumbers::ILatticeRsg^ Cephei::QL::Math::Randomnumbers::CLatticeRsg_Factory::Create (UInt64 dimensionality, Cephei::IVector<Double>^ z, UInt64 N)
{
    return gcnew CLatticeRsg ( dimensionality,  z,  N);
}
